package com.itheima.server.service;

import com.github.tobato.fastdfs.domain.conn.FdfsWebServer;
import com.github.tobato.fastdfs.domain.fdfs.StorePath;
import com.github.tobato.fastdfs.service.FastFileStorageClient;
import com.itheima.commons.utils.Constants;
import com.itheima.dubbo.api.PeachblossomApi;
import com.itheima.dubbo.api.UserInfoApi;
import com.itheima.model.domain.UserInfo;
import com.itheima.model.mongo.Peachblossom;
import com.itheima.model.vo.ErrorResult;
import com.itheima.model.vo.PeachblossomVo;
import com.itheima.server.exception.BusinessException;
import com.itheima.server.interceptor.UserHolder;
import org.apache.commons.lang3.StringUtils;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.HashOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class PeachblossomService {
    @DubboReference
    private PeachblossomApi peachblossomApi;
    @Autowired
    private FastFileStorageClient client;
    @Autowired
    private FdfsWebServer webServer;
    @Autowired
    private RedisTemplate<String, String> redisTemplate;
    @DubboReference
    private UserInfoApi userInfoApi;

    /*
    保存语音*/
    public void upload(MultipartFile soundFile) throws IOException {
        if (soundFile.isEmpty()) {
            throw new BusinessException(ErrorResult.error());
        }
        //将视频文件上传到fastDFS并获取url
        String fileName = soundFile.getOriginalFilename().substring(soundFile.getOriginalFilename().lastIndexOf(".") + 1);
        StorePath storePath = client.uploadFile(soundFile.getInputStream(), soundFile.getSize(), fileName, null);
        String soundUrl = webServer.getWebServerUrl() + storePath.getFullPath();
        //封装videos对象并保存。
        Peachblossom peachblossom = new Peachblossom();
        peachblossom.setUserId(UserHolder.getUserId());
        peachblossom.setSoundUrl(soundUrl);
        String soundId = peachblossomApi.save(peachblossom);
        if (StringUtils.isEmpty(soundId)) {
            throw new BusinessException(ErrorResult.error());
        }
    }

    public PeachblossomVo findOne() {
        //从redis中读取该用户已经读看过的数据的lid
        String key = Constants.SOUND;
        String hashKey = Constants.SOUNDS + UserHolder.getUserId();
        HashOperations<String, String, String> opsForHash = redisTemplate.opsForHash();
        String hasSound = opsForHash.get(key, hashKey);
        List<Long> lids = new ArrayList<>();
        if (!StringUtils.isEmpty(hasSound)) {
            String[] split = hasSound.split(",");
            lids = Arrays.stream(split).map(it -> Long.valueOf(it)).collect(Collectors.toList());
        }
        Peachblossom peachblossom = peachblossomApi.queryExtendLids(lids);
        if (peachblossom == null) {
            return null;
        }
        UserInfo userInfo = userInfoApi.findById(peachblossom.getUserId());
        //lids添加到redis中
        hasSound =StringUtils.isEmpty(hasSound)?peachblossom.getLid().toString(): hasSound + "," + peachblossom.getLid();
        opsForHash.put(key, hashKey, hasSound);

        return PeachblossomVo.init(userInfo, peachblossom);

    }
}
